const { Server } = require("socket.io");

const io = new Server({ cors: true });

const { port, game } = require("./server/config/config.json");
const db_handler = require('./server/db_handler.js');
const Handler = require("./server/handler.js");
const { logger } = require("./server/logger.js");

const eventBonus = require("./server/config/eventBonus.json");

const actors = new Map();

io.on("connection", (socket) => {

    //事件奖励完成
    socket.on("event complete", (eventId) => {
        // const actor = actors.get(socket.actorUid);
        const data = eventBonus[eventId];
        if (data) {
            // actor.inspectEvent(data);
            //临时
            socket.emit("gain bonus", data);
            logger.info(`玩家 sun 完成 ${eventId}事件 获得了奖励[exp1000]`)
        }

    })



    // //登录
    // socket.on("start", (identity) => {
    //     const name = Handler.getActorId(identity);
    //     socket.actorName = name;
    //     if (actors.has(name)) {
    //         const { battlerName, classId, level, exp, equips, skills, abilityPoints } = actors.get(name)
    //         const data = { battlerName, classId, level, exp, equips, skills, abilityPoints };
    //         socket.emit("actor data", data);
    //         return;
    //     }
    //     db_handler.getActorData(name, socket, DataServer.sendActorData);
    // })

    // //更新点数
    // socket.on("update points", (points) => {
    //     const actor = actors.get(socket.actorName);
    //     const newPoints = actor.updatePoints(points);
    //     if (newPoints) {
    //         socket.emit("update points result", newPoints);
    //     }
    // })

    // //经验值改变
    // socket.on("change exp", (data) => {
    //     const actor = actors.get(socket.actorName);
    //     actor.exp = data.exp;
    //     actor.level = data.evel;
    //     db_handler.updateExp(socket.actorName, data)
    // })

    // //装备改变
    // socket.on("change equip", (data) => {
    //     const actor = actors.get(socket.actorName);
    //     const { slotId, itemId } = data;
    //     actor.equips[slotId] = itemId;
    // })
})

io.listen(6545)

class DataServer {
    static sendActorData(data, socket) {
        socket.emit("actor data", data);
        const actor = new Actor(data);
        actors.set(socket.actorName, actor);
    }
}

class Actor {
    constructor(data) {
        // const { battlerName, classId, level, exp, equips, skills, abilityPoints } = data;
        // this.battlerName = battlerName;
        // this.classId = classId;
        // this.level = level;
        // this.exp = exp;
        // this.equips = equips;
        // this.skills = skills;
        // this.abilityPoints = JSON.parse(abilityPoints) ?? [0, 0, 0, 0, 0];
        this._socket = socket;
    }

    inspectEvent(data) {
        const { repeat, bonus, requirement } = data;
        //如果是一次性，先判定是否领取过
        if (!repeat) {
            //暂留
        }
        //检验条件是否达成
        if (requirement) {

        }
        this.gainBonus(bonus);
    }

    gainBonus(bonus) {
        this._socket.emit("gain bonus", bonus);
    }

    updatePoints(points) {
        //初始点数
        const inip = 10;
        //升级获得点数
        const ipp = 3;
        let sump = 0;
        for (const p of this.abilityPoints) {
            sump += p;
        }
        for (const p of points) {
            sump += p;
        }
        if (sump > (this.level - 1) * 3 + 10) {
            return false;
        }
        for (let i = 0; i < 5; i++) {
            this.abilityPoints[i] += points[i];
        }
        return this.abilityPoints;
    }
}